<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>返回顶部示例</title>
  <style>
    #topButton {
      display: none;
      position: fixed;
      bottom: 20px;
      right: 30px;
      z-index: 99;
      font-size: 16px;
      border: none;
      outline: none;
      background-color: rgba(0, 0, 0, 0.5);
      color: white;
      cursor: pointer;
      padding: 15px;
      border-radius: 4px;
    }
  </style>
</head>
<body>
  <button id="topButton">回到顶部</button>

  <!-- 大量内容，模拟长页面 -->
  <div style="height:2000px;">
    <p>滚动到页面底部...</p>
  </div>

  <script>
    window.addEventListener('scroll', function() {
      var topButton = document.getElementById('topButton');
      if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        topButton.style.display = 'block';
      } else {
        topButton.style.display = 'none';
      }
    });

    document.getElementById('topButton').addEventListener('click', function() {
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      });
    });
  </script>
</body>
</html>